package com.ruicar.afs.cloud.manage.clearance.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.clearance.vo.CostExemptBillManageVo;
import com.ruicar.afs.cloud.manage.common.entity.ManageCostExemptBill;
import com.ruicar.afs.cloud.manage.common.service.ManageCostExemptBillService;
import com.ruicar.afs.cloud.manage.workflow.processor.ApproveTaskProcessor;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author HuangTao
 * @description: 豁免单申请列表查询
 * @Date 2020/7/11 15:27
 */
@Slf4j
@RestController
@RequestMapping("/exemptSingleBill")
@AllArgsConstructor
public class CostExemptBillManageController {

    private final ManageCostExemptBillService manageCostExemptBillService;
    private final ApproveTaskProcessor approveTaskProcessor;

    @ApiOperation(value = "豁免单申请列表查询")
    @PostMapping("/findCostExemptBillList")
    public IResponse findCostExemptList(@RequestBody CostExemptBillManageVo vo) {
        vo.setProcessPerson(SecurityUtils.getUser().getUserRealName());
        Page page = new Page(vo.getPageNumber(), vo.getPageSize());
        IPage list = manageCostExemptBillService.getCostExemptBill(page, vo);
        list.setRecords(approveTaskProcessor.handle(list.getRecords()));
        return new IResponse().setData(list);
    }

    @Transactional
    @SysLog("根据ID删除费用豁免单")
    @ApiOperation(value = "通过id删除")
    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    public IResponse<Boolean> delById(@PathVariable String id) {
        manageCostExemptBillService.removeById(id);
        return new IResponse<Boolean>().setMsg("删除成功");
    }

    @ApiOperation(value = "豁免单保存")
    @PostMapping("/addCostExemptBill")
    public IResponse addCostExemptBill(@RequestBody CostExemptBillManageVo vo) {

        CostExemptBillManageVo costExemptBillManageVo = manageCostExemptBillService.addCostExemptBill(vo);
        return new IResponse<ManageCostExemptBill>().setData(costExemptBillManageVo);
    }

    @PostMapping("/submitCostExemptBill")
    @ApiOperation(value = "费用豁免单提交")
    @Transactional(rollbackFor = Exception.class)
    public IResponse submitCostExemptBill(@RequestBody CostExemptBillManageVo vo) throws JsonProcessingException {
        manageCostExemptBillService.submitCostExemptBill(vo);
        return new IResponse<Boolean>().setData(true).setMsg("流程提交成功！");
    }

    @PostMapping("/saveProposalApplicationInfo")
    @ApiOperation(value = "保存费用豁免单审批操作")
    @Transactional(rollbackFor = Exception.class)
    public IResponse saveProposalApplicationInfo(@RequestBody CostExemptBillManageVo vo) throws JsonProcessingException {
        manageCostExemptBillService.saveProposalApplicationInfo(vo);
        return new IResponse<Boolean>().setData(true).setMsg("流程提交成功！");
    }

    @PostMapping("/fingCostExemptBill")
    @ApiOperation(value = "获取以保存的数据")
    public IResponse<ManageCostExemptBill> fingCostExemptBill(@RequestParam("contractNo") String contractNo) throws JsonProcessingException {
        ManageCostExemptBill manageCostExemptBill = manageCostExemptBillService.getOne(Wrappers.<ManageCostExemptBill>lambdaQuery()
                .eq(ManageCostExemptBill::getContractNo, contractNo));
        return new IResponse<ManageCostExemptBill>().setData(manageCostExemptBill).setMsg("获取以保存的数据成功！");
    }
}
